package com.xlong.k329;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.xlong.k329.databinding.FragmentSecondBinding;

import java.sql.Connection;
import java.sql.SQLException;

public class SecondFragment extends Fragment {
    private FloatingActionButton btn_fab;
    private FragmentSecondBinding binding;
    private static final int MYSQL_CONN_OK = 0x00B;
    private static final int MYSQL_CONN_ERR = 0x00C;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentSecondBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.txtIp.setText(MainActivity.helper.getString("dbip"));
        binding.txtPort.setText(MainActivity.helper.getString("dbport"));
        binding.txtDbname.setText(MainActivity.helper.getString("dbname"));
        btn_fab = (FloatingActionButton)getActivity().findViewById(R.id.fab);
        btn_fab.setVisibility(View.GONE);
        binding.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存数据库配置
                MainActivity.helper.putValues(
                        new SharedPreferencesUtils.ContentValue("dbip", binding.txtIp.getText().toString()),
                        new SharedPreferencesUtils.ContentValue("dbport", binding.txtPort.getText().toString()),
                        new SharedPreferencesUtils.ContentValue("dbname", binding.txtDbname.getText().toString()));

                NavHostFragment.findNavController(SecondFragment.this)
                        .navigate(R.id.action_SecondFragment_to_FirstFragment);
            }
        });

        binding.btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(SecondFragment.this)
                        .navigate(R.id.action_SecondFragment_to_FirstFragment);
            }
        });

        binding.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //测试数据连接
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Connection connection = MainActivity.getConn(binding.txtIp.getText().toString(),binding.txtPort.getText().toString(),binding.txtDbname.getText().toString());
                            if(connection != null){
                                //Utils.toast(view.getContext(), "连接成功!");
                                mHandler.obtainMessage(MYSQL_CONN_OK).sendToTarget();
                                connection.close();
                            }else{
                                mHandler.obtainMessage(MYSQL_CONN_ERR).sendToTarget();
                                //Utils.toast(view.getContext(), "连接失败!");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MYSQL_CONN_OK:
                    Utils.toast(getActivity().getApplicationContext(), "连接成功!");
                    break;
                case MYSQL_CONN_ERR:
                    Utils.toast(getActivity().getApplicationContext(), "连接失败!");
                    break;

            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}